package demo.inoutput;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class CopyFile {
	private static String p;
	private static String copName;
	public static void main(String[] args) {
		File f = new File("C:/Users/TayDo/Demo");
		if (f.isFile()) {
			System.exit(0);
		}
		String path = f.getAbsolutePath();
		path += "/" + f.getName();
		path += "_Copy";
		copName = f.getName();
		copName += "_Copy";
		p = path;
		File fc = new File(path);
		fc.mkdirs();
		long start = System.currentTimeMillis();
		copyD(f);
		long end = System.currentTimeMillis();
		System.out.println("Time run: " + (end - start));
	}

	public static void copyD(File f) {
		try {
			String[] arrSubFile = f.list();
			String path = f.getAbsolutePath();
			if (arrSubFile != null) {
				for (int i = 0; i < arrSubFile.length; i++) {
					String fileName = arrSubFile[i];
					if (fileName.equals(copName)) {
						continue;
					}
					File sf = new File(path + "/" + fileName);
					File csf = new File(p + "/" + fileName);
					if (sf.isDirectory()) {
						csf.mkdirs();
						copyD(sf);
					} else {
						FileInputStream fis = new FileInputStream(sf);
						FileOutputStream fos = new FileOutputStream(csf);
						
						DataInputStream dis = new DataInputStream(fis);
						DataOutputStream dos = new DataOutputStream(fos);
						byte[] b = new byte[8024];
						 int length;
				         while ((length = dis.read(b)) > 0) {
				             dos.write(b, 0, length);
				         }
				         dis.close();
				         dos.close();
				         fis.close();
				         fos.close();
					}
					
//					Move
//					sf.delete();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
